import getdatah
import pandas as pd
import matplotlib.pyplot as plt
from flask import Flask, render_template, request
import plotly.graph_objs as go
import pandas as pd

df = getdatah.translate_column_names('合肥')
# 日期

app = Flask(__name__,template_folder='templates')


# 获取所有的列名
columns = list(df.columns)

@app.route('/visual')
def index():
    return render_template('weatherv3/index.html', columns=columns)

@app.route('/visual/plot', methods=['POST'])
def plot():
    # 获取用户选择的列名和图表类型
    column_name = request.form['column']
    chart_type = request.form['chart']

    # 从数据中选取指定的列
    data = df[column_name]

    # 根据图表类型绘制图表
    if chart_type == 'line':
        plt.plot(data)
    elif chart_type == 'bar':
        plt.bar(range(len(data)), data)

    # 将图表保存为图片
    plt.savefig('weatherv3/chart.png')

    # 清空当前的图表
    plt.clf()

    # 返回生成的图表
    return render_template('weatherv3/plot.html')

if __name__ == '__main__':
    app.run(debug=True)